package cn.kow.kmall.system.service.impl;

import cn.kow.kmall.common.constant.KmallConstants;
import cn.kow.kmall.common.enums.KmallResultEnum;
import cn.kow.kmall.core.exception.ServiceException;
import cn.kow.kmall.system.entity.SysDict;
import cn.kow.kmall.system.entity.SysDictData;
import cn.kow.kmall.admin.pojo.dto.DictDataQueryDTO;
import cn.kow.kmall.admin.pojo.dto.DictQueryDTO;
import cn.kow.kmall.system.repository.SysDictDataRepository;
import cn.kow.kmall.system.repository.SysDictRepository;
import cn.kow.kmall.system.service.SysDictService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: lijun
 * @Description:
 * @Date: Created in 2019-10-29 18:27
 * @Modified By:
 */
@Slf4j
@Service("sysDictService")
public class SysDictServiceImpl implements SysDictService {

    @Autowired
    private SysDictRepository sysDictRepository;

    @Autowired
    private SysDictDataRepository sysDictDataRepository;

    @Override
    public Page<SysDict> listDictByPage(DictQueryDTO queryDTO) {
        return sysDictRepository.queryByPage(queryDTO);
    }

    @Override
    public List<SysDict> listDict() {
        return sysDictRepository.listAll(new SysDict());
    }

    @Override
    public SysDict getById(Long dictId) {
        return sysDictRepository.getById(dictId);
    }

    @Override
    public SysDict getByType(String dictType) {
        SysDict sysDict = new SysDict();
        sysDict.setDictType(dictType);
        sysDict.setDeleted(false);
        return sysDictRepository.findOne(Example.of(sysDict))
                .orElseThrow(() -> new ServiceException(KmallResultEnum.DAO_NOT_FOUND));
    }

    @Override
    public Boolean checkTypeUnique(String dictType) {
        try {
            SysDict sysDict = getByType(dictType);
            if (sysDict == null) {
                return true;
            }
        } catch (ServiceException e) {
            if (e.getCode() == 404) {
                return true;
            }
        }
        return false;
    }

    @Override
    public void save(SysDict dict) {
        if (!checkTypeUnique(dict.getDictType())) {
            throw new ServiceException(KmallConstants.FAIL, "新增字典'" + dict.getDictName() + "'失败，字典类型已存在");
        }
        sysDictRepository.create(dict);
    }

    @Override
    public void update(SysDict dict) {
        SysDict sysDict = sysDictRepository.getById(dict.getId());
        if (!sysDict.getDictType().equals(dict.getDictType()) && !this.checkTypeUnique(dict.getDictType())) {
            throw new ServiceException(KmallConstants.FAIL,"修改字典'" + dict.getDictName() + "'失败，字典类型已存在");
        }
        sysDictRepository.update(dict);
    }

    @Override
    public void remove(List<Long> idList) {
        sysDictRepository.deleteAllById(idList);
    }

    @Override
    public Page<SysDictData> listDictDataByPage(DictDataQueryDTO queryDTO) {
        return sysDictDataRepository.queryByPage(queryDTO);
    }

    @Override
    public SysDictData getDictDataById(Long id) {
        return sysDictDataRepository.findById(id)
                .orElseThrow(() -> new ServiceException(KmallResultEnum.DAO_NOT_FOUND));
    }

    @Override
    public List<SysDictData> listDictDataByType(String dictType) {
        SysDictData sysDictData = new SysDictData();
        sysDictData.setDictType(dictType);
        return sysDictDataRepository.listAll(sysDictData);
    }

    @Override
    public void addDictData(SysDictData dictData) {
        dictData.setIsDefault("N");
        sysDictDataRepository.create(dictData);
    }

    @Override
    public void deleteDictDataByIds(List<Long> idList) {
        sysDictDataRepository.deleteAllById(idList);
    }

    @Override
    public void updateDictData(SysDictData dictData) {
        sysDictDataRepository.update(dictData);
    }

}
